/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Shared/Binary/WorldPacketDump.h"
#include "Shared/Util/String.h"
#include "Shared/Log/Log.h"


namespace Shared {
namespace Binary {

    void dumpWorldPacket(int connId, const WorldPacket *packet)
    {
        std::string packet_str("");
        if (packet->size() > 0)
        {
            const std::uint8_t *byte_ptr = packet->contents();
            std::size_t p = 0;
            while (true)
            {
                for (size_t j = 0; j < 16 && p < packet->size(); ++j) {
                    char buf[0x10] = { 0 };
                    Util::snprintf(buf, sizeof(buf), "%.2X ", byte_ptr[p++]);
                    packet_str.append(buf);
                }

                if (p < packet->size()) {
                    packet_str.append("\n");
                    continue;
                }
                break;
            }
        }
        sharedLog.outDebug("dumpWorldPacket() %d\n"
            "cmd:0x%x\n"
            "size: %d\n"
            "===============================================\n"
            "%s"
            "\n===============================================",
            connId, packet->opcode(), packet->size(), packet_str.c_str());
    }

}}